const output = document.querySelector('.output');
const numberButtons = Array.from(document.querySelectorAll('.number button'));
const acButton = numberButtons.find(button => button.textContent === "AC");
const cButton = numberButtons.find(button => button.textContent === "C");
const equalsButton = numberButtons.find(button => button.textContent === "=");

numberButtons.forEach(button => {
    button.addEventListener('click', () => {
        if (button.textContent !== 'AC' && button.textContent !== 'C' && button.textContent !== '=') {
            output.value += button.textContent;
        }
    });
});

acButton.addEventListener('click', () => {
    output.value = '';
});

cButton.addEventListener('click', () => {
    output.value = output.value.slice(0, -1);
});

function calculateExpression(expression) {
    if (expression.includes('(')) {
        let left = expression.indexOf('(');
        let right = expression.indexOf(')');
        let subExp = expression.substring(left + 1, right);
        let subResult = calculateExpression(subExp);
        expression = expression.substring(0, left) + subResult + expression.substring(right + 1);
        return calculateExpression(expression);
    }

    const operators = {
        '+': 1,
        '-': 1,
        '×': 2,
        '÷': 2,
    };

    let operatorStack = [];
    let numberStack = [];
    let number = '';

    for (let i = 0; i < expression.length; i++) {
        let char = expression[i];
        if (!operators[char]) {
            number += char;
            continue;
        } else {
            if (char === '-' && (i === 0 || expression[i - 1] === '(')) {
                number += char;
                continue;
            }
            numberStack.push(parseFloat(number));
            number = '';
        }

        while (operatorStack.length && operators[char] <= operators[operatorStack[operatorStack.length - 1]]) {
            calc(numberStack, operatorStack);
        }

        operatorStack.push(char);
    }

    numberStack.push(parseFloat(number));

    while (operatorStack.length) {
        calc(numberStack, operatorStack);
    }

    return numberStack[0];
}

function calc(numberStack, operatorStack) {
    const number2 = numberStack.pop();
    const number1 = numberStack.pop();
    const operator = operatorStack.pop();
    let result;

    switch (operator) {
        case '+':
            result = number1 + number2;
            break;
        case '-':
            result = number1 - number2;
            break;
        case '×':
            result = number1 * number2;
            break;
        case '÷':
            result = number1 / number2;
            break;
    }

    numberStack.push(result);
}

equalsButton.addEventListener('click', () => {
    const expression = output.value;
    const result = calculateExpression(expression);
    output.value = result;
});

const Convertprompt = document.querySelector('.convert .prompt');
const convertedInput = document.getElementById('converted');
const closeIcon = document.querySelector('.convert .prompt i');

closeIcon.addEventListener('click', () => {
    Convertprompt.style.display = 'none';
});

const ThemeColorcancel = document.querySelector('.color-cancel');
const ThemeColorprompt = document.querySelector('.color-confirm');

ThemeColorcancel.addEventListener('click', () => {
    ThemeColorprompt.style.display = 'none';
});

ThemeColorprompt.addEventListener('click', () => {
    ThemeColorprompt.style.display = 'none';
});

window.onload = initTheme;